import Vue from 'vue'
import Vuex from 'vuex'
import * as db from '../util/db.js'
Vue.use(Vuex)
let userInfo = db.get('userInfo') || {}
let token = db.get('token') || ''
let motionDataList = (() => {
	const dateOfToday = Date.now()
	const dayOfToday = (new Date().getDay() + 7 - 1) % 7
	const daysOfThisWeek = Array.from(new Array(7))
		.map((_, i) => {
			const date = new Date(dateOfToday + (i - dayOfToday) * 1000 * 60 * 60 * 24)
			return date.getFullYear() +
				'-' +
				String(date.getMonth() + 1).padStart(2, '0') +
				'-' +
				String(date.getDate()).padStart(2, '0')
		})
	let list = []
	daysOfThisWeek.forEach((date, index) => {
		list.push({
			date: date,
			week: index + 1,
			motion: [{
					type: 'breakfast',
					motionList: []
				},
				{
					type: 'lunch',
					motionList: []
				},
				{
					type: 'dinner',
					motionList: []
				}
			]
		})
	})
	return list
})()
const store = new Vuex.Store({
	state: {
		userInfo: userInfo,
		token: token,
		//周计划缓存
		motionDataList: motionDataList
	},
	mutations: {
		saveUserTokenAction(state, token) {
			state.token = token;
			db.set('token', state.token)
		},
		setUserInfoMutations(state, userinfo) {
			state.userInfo = userinfo;
			db.set('userInfo', state.userInfo)
		},
		setmotionDataListMutations(state, motionDataList) {
			state.motionDataList = motionDataList;
		},
		setmotionListByDayMutations(state, data) {
			let {
				list,
				week
			} = data
			let obj = state.motionDataList.find((item, index) => {
				return item.week == week
			})
			obj.motion = list
		},
		setmotionListByDaytypeMutations(state, data) {
			let {
				list,
				week,
				type
			} = data
			let obj = state.motionDataList.find((item, index) => {
				return item.week == week
			}).motion

			let info = obj.find(item => {
				return item.type == type
			})

			info.motionList = list
		}
	},
	actions: {
		saveUserTokenAction(state, token) {
			state.commit('saveUserTokenAction', token)
		},
		setUserInfoAction(state, userinfo) {
			state.commit('setUserInfoMutations', userinfo)
		},
		setmotionDataListAction(state, motionDataList) {
			state.commit('setmotionDataListMutations', motionDataList)
		},
		setmotionListByDayAction(state, data) {
			state.commit('setmotionListByDayMutations', data)
		},
		setmotionListByDaytypeAction(state, data) {
			state.commit('setmotionListByDaytypeMutations', data)
		}
	},
	getters: {
		getUserInfos(state) {
			return state.userInfo;
		},
		getisLogin(state) {
			if (JSON.stringify(state.userInfo) == "{}") {
				return false;
			} else {
				return true;
			}
		},
		getmotionDataList(state) {
			return state.motionDataList;
		},
		getmotionListByDay(state) {
			return function(week) {
				// console.log(state, week)
				let list = state.motionDataList.find((item, index) => {
					return item.week == week
				})
				return list
			}
		},
		getmotionListByDaytype(state) {
			return function(week, type) {
				let list = (state.motionDataList.find((item, index) => {
					return item.week == week
				})).motion
				return list.find(item => {
					return item.type == type
				})
			}
		}
	},
	modules: {

	}
})

export default store
